package com.optionexplorer.strategies;

import java.util.ArrayList;
import java.util.Date;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.optionexplorer.data.CallOption;
import com.optionexplorer.data.DateOptionFilter;
import com.optionexplorer.data.PutOption;
import com.optionexplorer.data.SimpleOptionFilter;
import com.optionexplorer.data.StockOption;
import com.optionexplorer.data.StrikePriceOptionFilter;
import com.optionexplorer.data.YahooOptionQuotes;

/**
 * Base class for the option strategies. Includes common methods to fetch data and filter it.
 * @author kgajjala
 *
 */
public class ShortCallShortPutStrategy {

	private String ticker = null;
	private int priceRange = 10;
	private int lookAhead = 90;
	
	private ArrayList<ArrayList<StockOption>> callOptionsList = null;
	private ArrayList<ArrayList<StockOption>> putOptionsList = null;
	private ArrayList<ShortCallShortPutPackage> pkgList = null;
	
	private JSONObject finalResultJSON = null;
	
	public ShortCallShortPutStrategy(String t,
			                         int priceRange,
			                         int lookAhead) {
		this.ticker = t;
		this.priceRange = priceRange;
		this.lookAhead = lookAhead;
	}

	public String getHeader() {
		StringBuilder sb = new StringBuilder();
		sb.append("Stock \t");
		sb.append("Call\t");
		sb.append("Put\t");
		sb.append("Amt. Ret\t");
		sb.append("%Ret\t");
		sb.append("%Annual Ret\t");
		sb.append("Assign Price\n");
				
		return sb.toString();
	}
	

	
	public void analyze() {
		
		ArrayList<StockOption> callList = null;
		ArrayList<StockOption> putList = null;
		JSONArray resultArrJSON = new JSONArray();
		int resultCount = 0;
		
		YahooOptionQuotes yoq = new YahooOptionQuotes(this.ticker);
		yoq.populateOptionMatrix();
		this.callOptionsList = yoq.getCallOptionsMatrix();
		this.putOptionsList = yoq.getPutOptionsMatrix();
		
		CallOption callOption = null;
		PutOption putOption = null;
		
		/* Filter for the look Ahead*/
		DateOptionFilter dof = new DateOptionFilter();
		dof.setDaysOut(this.lookAhead);
		
		/* Filter for the strike price*/
		StrikePriceOptionFilter spof = new StrikePriceOptionFilter();
		spof.setPriceRange(this.priceRange);
		
		int numSets = this.callOptionsList.size();
		ShortCallShortPutPackage scsp = null;
		
		for (int i=0; i<numSets; i++) {
			callList = callOptionsList.get(i);
			putList = putOptionsList.get(i);
			
			/* Filter calls first with date and then with strike price*/
			callList = dof.filterData(callList);
			callList = spof.filterData(callList);

			/* Filter puts first with date and then with strike price*/
			putList = dof.filterData(putList);
			putList = spof.filterData(putList);
			
			if (callList.isEmpty() || putList.isEmpty()) {
				continue;
			}
			int callSize = callList.size();
			int putSize = putList.size();
			
			for (int j=0; j<callSize && j<putSize; j++) {
				callOption = (CallOption) callList.get(j);
				putOption = (PutOption) putList.get(j);

				/* For now use the combos with same strike price*/
				if (callOption.getStrikePrice() == putOption.getStrikePrice()) {
					scsp = new ShortCallShortPutPackage();
					scsp.setStock(this.ticker,callOption.getStockPrice(),100);
					scsp.addCall(callOption);
					scsp.addPut(putOption);
					scsp.computeReturns();
					//System.out.println(scsp);
					resultArrJSON.add(scsp.toJSON());
				}
								
			}
			
		}
		
		JSONObject obj = new JSONObject();
		obj.put("Result", resultArrJSON);
		obj.put("totalResultsAvailable", resultArrJSON.size());
		obj.put("totalResultsReturned", resultArrJSON.size());
		obj.put("firstResultPosition", 1);
		
		finalResultJSON = new JSONObject();
		finalResultJSON.put("ResultSet", obj);

		return;
	}
	
	public JSONObject getResultsJSON() {
		return finalResultJSON;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ShortCallShortPutStrategy scss = new ShortCallShortPutStrategy("CSTR", 15, 180);
		scss.analyze();
		System.out.println(scss.getResultsJSON());
		
	}

}
